/*
 Lightweight syntax-analyzing script
 Copyright: modified BSD license 2008 martin
   version: 20090406.024752
*/

function pettieSyntax(el){
 var d = document, sheet = d.styleSheets && d.styleSheets[0];
 if(!d.getElementById('syntax-css') && sheet){
  var synRules = {
   'synCss' : 'color: #603;',  'synAtr' : 'color: #369;',  'synVal' : 'color: #663;',
   'synStr' : 'color: darkred;',
   'synOpe' : 'color: #066;',
   'synVar' : 'color: #c33;',
   'synTag' : 'color: #639;',
   'synKwd' : 'color: #f03;',
   'synEqu' : 'color: navy;',
   'synRex' : 'color: darkorange;',
   'synHlp' : 'color: #390;',
   'synLin' : 'color: #663;',
   'synBrc' : 'color: #966;'
  };
  for (var i in synRules){
   if(sheet.addRule){
    sheet.addRule('span.' + i, synRules[i]);
   } else sheet.insertRule('span.' + i + '{' + synRules[i] + '}', sheet.cssRules.length);
  }
 }
 var cssprops = ['synCss', 'synAtr', 'synVal', /([ :\[\]#\*a-z0-9>\.,_="~|$+-]+?)\{([^}]+?)\}/ig];
 var etcetera = ['synStr', /( &gt; | &lt; )/g];
 var operants = ['synOpe', /(\/\/\-\-&gt;|\+\+|\-\-|!?===?|&lt;=|&gt;=|=&gt;|\+=|\-=|!=|&amp;&amp;|\|\|| = )/g];
 var variants = ['synVar', /(\$\w+?\b|var\b\w+\b|@\w+?\b)/g];
 var htmltags = ['synTag', /(&lt;\/\w+?&gt;|&lt;\w+|&lt;\?(?:xml|php)|(?:\?|\/)&gt;|&gt;)/g];
 var keywords = ['synKwd', /(\b(?:alert|[Aa]rray|break|case|catch|class|charset|continue|Date|default|define|delete|do|else|false|FALSE|for|function|global|i[fn]|int|instanceof|javascript|new|null|Object|return|script|src|style|href|type|switch|this|throw|document\S|TRUE|true|echo|try|typeof|var|void|while|window\S|with|Location|version|[^$]encoding)\b)/g];
 var equipped = ['synEqu', /[.@(\r\n ]([a-z_,]{3,})[=();{? ]+?[^a-z0-9{,]/ig];
 var oneliner = ['synLin', /[^'"]('|")[^\r\n'"]+?\1/g];
 var regulars = ['synRex', /(\b\/[^*][^\s]+?[^*/]\/[igm.]\b)/g];
 var comments = ['synHlp', /[^:]\/\/.+|[^\f:"'\|\s-]#\s+?[^\t'",]+|\/\*[^\^]+?\*\/|&lt;!\-\-.+?\-\-&gt;/g];
 var omitspan = function(m){ return m.replace(/\t\f\t[^\t]+?\t\t/g, '').replace(/\f/g, '');}
 var brackets = ['synBrc', /([\(\{\[\)\}\]])/g];
 var targets = (el || d).getElementsByTagName('pre');
 if(targets){
  for(var t, i = 0; t = targets[i++];){
   if(t.className && t.className == "thru") continue;
   var c = t.innerHTML.replace(/<(?:span|code)[^>]*?>([^<]+?)<\/(?:span|code)>/ig, '$1').replace(/\t/g, '');
   if(/<(?:a |img )/i.test(c)) continue;
   c = c.replace(cssprops[3], function(A, B, C){ return /:/.test(C) ? '\t\f\t'+cssprops[0]+'\t\t'+B+'\f{' + C.replace(/([^:]+?):([^;]+?);/g, '\t\f\t'+cssprops[1]+'\t\t'+'$1\f:' + '\t\f\t'+cssprops[2]+'\t\t' + '$2\f;') + '}' : A;});
   c = c.replace(variants[1], '\t\f\t'+variants[0]+'\t\t$1\f');
   c = c.replace(htmltags[1], '\t\f\t'+htmltags[0]+'\t\t$1\f');
   c = c.replace(keywords[1], '\t\f\t'+keywords[0]+'\t\t$1\f');
   c = c.replace(equipped[1], function (A, B){ return A.replace(B, '\t\f\t'+equipped[0]+'\t\t' + B + '\f');});
   c = c.replace(operants[1], '\t\f\t'+operants[0]+'\t\t$1\f');
   c = c.replace(regulars[1], function (A){ return '\t\f\t'+regulars[0]+'\t\t' + omitspan(A) + '\f';});
   c = c.replace(oneliner[1], function (A){ return A.slice(0, 1) + '\t\f\t'+oneliner[0]+'\t\t' + omitspan(A.slice(1)) + '\f';});
   c = c.replace(comments[1], function (A){ return '\t\f\t'+comments[0]+'\t\t' + omitspan(A) + '\f';});
   c = c.replace(/\t\f\t/g, '<span class="').replace(/\t\t/g, '">').replace(/\f/g, '</span>');
   c = c.replace(brackets[1], '<span class="synBrc">$1</span>');
   
  /* c = '<ol>\n' + c;
   c = c.replace(/\n?(.*?)\n/gm, '<li>$1</li>');
   c += '</ol>';
   */
   if(/*@cc_on!@*/false){
    t.outerHTML = '\n<pre>' + c + '</pre>\n';
   } else t.innerHTML = c;
  };
 }
};
